import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:flutter/material.dart';

import '../../../../dialogs/active_player_confirmation.dart';
import '../../../../data/controllers/gyms_controller.dart';
import '../../../../dialogs/delete_player_confirmation.dart';
import '../../add_gym_page/bloc/add_gym_page_screen.dart';
import '../../gym_info_page/gym_info_screen.dart';
import '../models/gym.dart';


class GymItemView extends StatefulWidget {
  final Gym gym;
  const GymItemView({super.key, required this.gym});

  @override
  State<GymItemView> createState() => _GymItemViewState();
}

class _GymItemViewState extends State<GymItemView> {
  @override
  Widget build(BuildContext context) {

    return InkWell(
      onTap: (){
         Navigator.push(context, MaterialPageRoute(builder: (context) => GymInfoScreen.builder(context, widget.gym,false)));
      },
      child: Slidable(
        enabled: PrefUtils.isGymAdmin(),
        // The start action pane is the one at the left or the top side.
        startActionPane: ActionPane(
          // A motion is a widget used to control how the pane animates.
          motion: const ScrollMotion(),

          children: [
            // A SlidableAction can have an icon and/or a label.
            SlidableAction(
              onPressed: (context){
                showDeleteConfirmation(context, widget.gym);
              },
              backgroundColor: Colors.transparent,
              foregroundColor: Colors.red,
              icon: Icons.delete,
              label: 'حذف',
            ),
            SlidableAction(
              onPressed: (context){
                Navigator.push(context, MaterialPageRoute(builder: (context) => AddGymPageScreen(gym:widget.gym,)));

              },
              backgroundColor: Colors.transparent,
              foregroundColor: Colors.orange,
              icon: Icons.edit,
              label: 'تعديل',
            ),
          ],
        ),
        endActionPane: ActionPane(
          // A motion is a widget used to control how the pane animates.
          motion: const ScrollMotion(),
          children: [
            if(widget.gym.active == 0)
            SlidableAction(
              onPressed: (context){
                showActiveConfirmation(context, widget.gym);
              },
              backgroundColor: Colors.transparent,
              foregroundColor: Colors.green,
              icon: Icons.check,
              label: 'تفعيل',
            )
            else
              SlidableAction(
                onPressed: (context){
                  showDisableConfirmation(context, widget.gym);
                },
                backgroundColor: Colors.transparent,
                foregroundColor: Colors.red,
                icon: Icons.cancel_outlined,
                label: 'تعطيل',
              ),
          ],
        ),
        child: Stack(
          children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Container(
                height: 220,
                decoration: BoxDecoration(
                  color: Theme.of(context).colorScheme.secondary,
                  image: DecorationImage(
                      fit:(widget.gym.image==null)? null:BoxFit.cover,
                      isAntiAlias: true,
                      onError: (obj , st){
                        setState(() {
                          widget.gym.image= null;
                        });
                      },
                      image:(widget.gym.image!=null)?CachedNetworkImageProvider(widget.gym.image!):Image.asset(ImageConstant.imgLogo).image
                  ),
                ),
                child: Container(
                  padding:const EdgeInsets.all(4),
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                      colors: [
                        Colors.black,
                        Colors.black.withOpacity(0),
                        Colors.black,
                      ],
                    ),
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Expanded(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Text(widget.gym.name, style: theme.textTheme.titleMedium?.copyWith(color: Colors.redAccent),),
                                    Row(
                                      children: [
                                        const SizedBox(width: 4,),
                                        const Icon(Icons.group,color: Colors.orangeAccent,),
                                        const SizedBox(width: 2,),
                                        Text(widget.gym.recentAttendanceCount.toString(), style: theme.textTheme.titleSmall?.copyWith(color: Colors.orangeAccent),),
                                      ],
                                    )
                                  ],
                                ),
                                SizedBox(
                                  child: Text(
                                    widget.gym.address,
                                    maxLines: 5,
                                    style: theme.textTheme.titleMedium!.copyWith(fontSize: 14,color: Colors.white),),
                                ),
                              ],
                            ),
                          ),

                        ],
                      ),
                      Align(
                        alignment: Alignment.centerRight,
                        child: Text(
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          widget.gym.description,
                          style: theme.textTheme.titleSmall?.copyWith(color: Colors.white),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),

            if(widget.gym.active ==1)
              Align(
                alignment: Alignment.topLeft,
                child:Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: InkWell(
                    onTap: ()async{
                      //onSelectedImage?.call((await _getImageFromGallery()));
                    },
                    child: Container(
                        decoration:BoxDecoration(
                          color: Colors.grey.withOpacity(.5),
                          shape: BoxShape.circle,
                        ),
                        child: Image.asset(ImageConstant.icActive,width: 32,)),
                  ),
                ),
              ),
            if(widget.gym.active ==0)
              Align(
                alignment: Alignment.topLeft,
                child:Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: InkWell(
                    onTap: ()async{
                      //onSelectedImage?.call((await _getImageFromGallery()));
                    },
                    child: Container(
                        decoration:BoxDecoration(
                          color: Colors.grey.withOpacity(.5),
                          shape: BoxShape.circle,
                        ),
                        child: Image.asset(ImageConstant.icWrong,width: 32,)),
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }

  void showDeleteConfirmation(BuildContext context, Gym myGym) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return DeleteConfirmationDialog(
          playerName: myGym.name,
          titleUser: 'النادي',
          onConfirm: () {
            GymsController.deleteById(widget.gym.gymId??'-1');
            if (kDebugMode) {
              print('تم حذف النادي ${widget.gym.name}!');
            }
          },
        );
      },
    );
  }

  void showActiveConfirmation(BuildContext context, Gym myGym) {
    showDialog(
      context: context,
      builder: (BuildContext context) {

        return ActiveConfirmationDialog(
          playerName: myGym.name,
          titleUser: 'تفعيل النادي',
          onConfirm: () {
            GymsController.updateGym(widget.gym.copyWith(active: 1));
            if (kDebugMode) {
              print('تم تفعيل النادي ${widget.gym.name}!');
            }
          },
        );
      },
    );
  }

  void showDisableConfirmation(BuildContext context, Gym myGym) {
    showDialog(
      context: context,
      builder: (BuildContext context) {

        return ActiveConfirmationDialog(
          playerName: myGym.name,
          titleUser: 'تعطيل النادي',
          onConfirm: () {
            GymsController.updateGym(widget.gym.copyWith(active: 0));
            if (kDebugMode) {
              print('تم تعطيل النادي ${widget.gym.name}!');
            }
          },
        );
      },
    );
  }
}
